#!/bin/bash

# 检查.env文件是否存在
if [ ! -f ".env" ]; then
    echo "错误: 未找到 .env 文件"
    echo "请确保 .env 文件存在并包含必要的环境变量"
    exit 1
fi

# 从.env文件加载环境变量
source .env

# 检查必要的环境变量
if [ -z "$OPENAI_API_KEY" ]; then
    echo "错误: OPENAI_API_KEY 未设置"
    exit 1
fi

echo "使用以下配置启动容器:"
echo "OPENAI_API_KEY: ${OPENAI_API_KEY:0:10}..."
echo "OPENAI_BASE_URL: $OPENAI_BASE_URL"
echo "MODEL_NAME: $MODEL_NAME"
echo "MAX_TOKENS: $MAX_TOKENS"
echo ""

# 停止并删除已存在的容器
docker stop chatbot 2>/dev/null || true
docker rm chatbot 2>/dev/null || true
$PIP_INDEX_URL="http://mirrors.aliyun.com/pypi/simple"
$PIP_TRUSTED_HOST="mirrors.aliyun.com"

# 启动新容器
docker run --name chatbot \
    -d \
    -p 8000:8000 \
    -e OPENAI_API_KEY="$OPENAI_API_KEY" \
    -e OPENAI_BASE_URL="$OPENAI_BASE_URL" \
    -e MODEL_NAME="$MODEL_NAME" \
    -e MAX_TOKENS="$MAX_TOKENS" \
    -e PIP_INDEX_URL="$PIP_INDEX_URL" \
    -e PIP_TRUSTED_HOST="$PIP_TRUSTED_HOST" \
    weinanhu/chatbot:latest

if [ $? -eq 0 ]; then
    echo "容器启动成功!"
    echo "访问地址: http://localhost:8000"
    echo "查看日志: docker logs -f chatbot"
else
    echo "容器启动失败!"
    exit 1
fi